package com.example.zhyq.park

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.example.base.entity.DeleteParkBean
import com.example.base.entity.ParamPark
import com.example.base.entity.ParkBeanItem
import com.example.base.mvvm.BaseViewModel
import com.example.base.net.ApiService
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow

import javax.inject.Inject

@HiltViewModel
class ParkViewModel @Inject constructor(val apiService: ApiService) : BaseViewModel() {

    fun getParkByType(para:String,type:String): Flow<PagingData<ParkBeanItem>> {
        return Pager(config = PagingConfig(pageSize = 10), pagingSourceFactory = {
            ParkDataSource(apiService,para,type)
        }).flow.cachedIn(viewModelScope)
    }


    var addParkSuc = MutableLiveData<ParkBeanItem>()
    fun addPark(beanItem: ParamPark) {
        httpRequest({apiService.addPark(beanItem)},{addParkSuc.postValue(it)})
    }

    var editParkSuc = MutableLiveData<ParkBeanItem>()
    fun editPark(id: Int,beanItem: ParkBeanItem) {
        httpRequest({apiService.editPark(id,toBody(beanItem))}, {editParkSuc.postValue(it)})
    }

    var deleteParkSuc = MutableLiveData<Boolean>()
    fun deletePark(id:Int) {
        httpRequest({apiService.deletePark(id)},{deleteParkSuc.postValue(it)})
    }
}